package com.telecom.taglibs;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;
import com.telecom.cms.dao.GetConnect;
import com.telecom.cms.dbmodel.DAO_BAK.SEG_BIZ_ATOM_DEF_NEWDAO;
import com.telecom.cms.dbmodel.DTO.SEG_BIZ_ATOM_DEF_NEWDTO;
import com.telecom.tree.TreeUtil;

/**
 * @author yueyingnishang
 * 
 */
public class InitTreeTag extends BodyTagSupport {

	private int property;

	public int getProperty() {
		return this.property;
	}

	public void setProperty(int property) {
		this.property = property;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#doAfterBody()
	 */
	public int doAfterBody() throws JspException {
		// TODO Auto-generated method stub
		Connection con = GetConnect.getConnection();
		SEG_BIZ_ATOM_DEFDAO dao = new SEG_BIZ_ATOM_DEFDAO(con);
		SEG_BIZ_ATOM_DEFDTO dto = dao.findById(property);
		BodyContent body = this.getBodyContent();
		StringBuffer st = new StringBuffer();
		st.append("<script type=\"text/javascript\">\n");
		st.append(TreeUtil.createTreeInfo(TreeUtil.retrieveNodeInfos(dto
				.getCDE_SRC_TABLE_NAME(), dto.getBIZ_ATOM_NAME()), dto
				.getBIZ_ATOM_NAME()));

		st.append("</script>\n");

		try {
			body.getEnclosingWriter().println(st.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return EVAL_PAGE;
	}

	// public int doEndTag() throws JspException {
	// StringBuffer tree = new StringBuffer();
	// tree.append("<script type=\"text/javascript\">\n");
	// tree.append(TreeUtil.createTreeInfo(TreeUtil.retrieveNodeInfos()));
	// tree.append("</script>\n");
	// try {
	// pageContext.getOut().println(tree.toString());
	// } catch (IOException ioe) {
	// ioe.printStackTrace();
	// }
	// // System.out.println(tree);
	// return super.doEndTag();
	// }

}
